package com.example.lijun.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.graphics.drawable.ViewUtils;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.util.Log;

import java.util.ArrayList;

import lombok.Data;
import lombok.EqualsAndHashCode;
import tools.Utils;

/**
 * Created by lijun on 2017/11/27.
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class LettersView extends AppCompatImageView {

    private final static String TAG_V = "LettersView";

    private ViewUtils viewUtils;
    private Paint paint;
    private Path path;
    private String fontResources;
    private Utils utils;

    private ArrayList<Path> letterPathList;
    private ArrayList<Character> letterList;


    //坐标系统相关参数

    //字符间距
    private int deltaX;
    //行 间距
    private int deltaY;

    //整个坐标系的参考原点
    private int originalPointX;
    private int originalPointY;

    //字符缩放比例
    private float scaleX;
    private float scaleY;

    //当前坐标点
    private float currentPointX;
    private float currentPointY;

    private boolean enterState;
    private boolean inputState;

    private char inputChar;

    private int lastEnterPosition;

    private ArrayList<Integer> enterPositionList;

    public LettersView(Context context) {
        super(context);
        init();
    }

    public LettersView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public LettersView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init() {
        initInstances();
        initParams();
        initPaint();

        enterState = false;
        //false代表正在输入 true代表输入完毕
        inputState = false;
    }

    public void initInstances() {
        paint = new Paint();
        path = new Path();
        viewUtils = new ViewUtils();
        letterPathList = new ArrayList<>();
        letterList = new ArrayList<>();
        enterPositionList = new ArrayList<>();
        utils = new Utils();
    }


    private void initParams() {
        //开始初始化绘图系统
        deltaX = 18;
        deltaY = 0;

        //初始化坐标原点
        originalPointX = 10;
        originalPointY = 50;

        //初始化到坐标原点
        currentPointX = originalPointX;
        currentPointY = originalPointY;

        //字符缩放比例
        scaleX = 3f;
        scaleY = 3f;

        //
        lastEnterPosition = 0;
    }

    private void initPaint() {
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2f);
    }

    //保留用户输入的字母
    public void getLetterPath(String letters) {
        String lettersPath;

        inputState = false;
        if (letters.length() <= 0) {
            return;
        }
        Log.d(TAG_V, "cccc" + letters.length());
        for (int i = 0; i < letters.length(); i++) {
            inputChar = letters.charAt(i);
            letterList.add(inputChar);
            //检测到回车
            if (inputChar == '\n') {
                //暂时使用A 代替换行
                lettersPath = utils.getLetterPath(fontResources, 'A');
                path = viewUtils.getLetterPath(lettersPath);
                letterPathList.add(path);
                continue;
            }
            lettersPath = utils.getLetterPath(fontResources, inputChar);
            try {
                path = viewUtils.getLetterPath(lettersPath);
                letterPathList.add(path);
            } catch (Exception e) {
                Log.d(TAG_V, " path parser error: " + e.getMessage());
            }
        }
        inputState = true;
        Log.d(TAG_V, " ccc " + letterPathList.size());
    }

    //letterPathList 里边放着的是所有字符的path
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(originalPointX, originalPointY);
        canvas.scale(scaleX, scaleY);

        Log.d(TAG_V, "xxx");

        if (inputState == true) {
            for (int i = 0; i < letterList.size(); i++) {
                if (letterList.get(i) == '\n') {
                    deltaY = 30;
                    enterPositionList.add(i);
                    if (lastEnterPosition == 0) {
                        canvas.translate(-deltaX * (i), deltaY);
                    } else {
                        lastEnterPosition = enterPositionList.get(enterPositionList.size() - 1) - enterPositionList.get(enterPositionList.size() - 2);
                        canvas.translate(-deltaX * lastEnterPosition + deltaX, deltaY);
                        continue;
                    }

                }
                deltaY = 0;
                canvas.translate(deltaX, deltaY);
                canvas.drawPath(letterPathList.get(i), paint);
            }
            letterPathList.clear();
            letterList.clear();
        }
    }
}
